<?php

/**
 * CurrencyHelper.
 *
 * @package    SchelkinoBiz
 * @subpackage helper
 * @author     Delight
 */

function format_currency($amount)
{
    if (null === $amount)
    {
        return null;
    }

    $user = sfContext::getInstance()->getUser();

    $currency_ratio = $user->hasAttribute( 'currency_ratio' ) ? $user->getAttribute( 'currency_ratio' ) : array();

    $currency       = $user->hasAttribute( 'currency' )       ? $user->getAttribute( 'currency' )       : sfConfig::get( 'app_default_currency' );

    if ( is_array( $amount ) )
        foreach ( $amount as $k => $item )
        {
            $amount[$k] = round( $item /= isset( $currency_ratio[ $currency ] ) ? $currency_ratio[ $currency ] : 1 );
        }
    else
        $amount = round( $amount /= isset( $currency_ratio[ $currency ] ) ? $currency_ratio[ $currency ] : 1 );

    return $amount;
}

function format_currency_base($amount)
{
    if (null === $amount)
    {
        return null;
    }

    $user = sfContext::getInstance()->getUser();

    $currency_ratio = $user->hasAttribute( 'currency_ratio' ) ? $user->getAttribute( 'currency_ratio' ) : array();

    $currency       = $user->hasAttribute( 'currency' )       ? $user->getAttribute( 'currency' )       : sfConfig::get( 'app_default_currency' );

    if ( is_array( $amount ) )
        foreach ( $amount as $k => $item )
        {
            $amount[$k] = round( $item *= isset( $currency_ratio[ $currency ] ) ? $currency_ratio[ $currency ] : 1 );
        }
    else
        $amount = round( $amount *= isset( $currency_ratio[ $currency ] ) ? $currency_ratio[ $currency ] : 1 );

    return $amount;
}

function format_currency_label()
{
    $currency = sfContext::getInstance()->getUser()->hasAttribute( 'currency' ) ? sfContext::getInstance()->getUser()->getAttribute( 'currency' ) : sfConfig::get( 'app_default_currency' );

    switch( $currency )
    {
        case 'USD':
            $label = "$";
            break;
        case 'EUR':
            $label = "€";
            break;
        case 'RUB':
            $label = "руб.";
            break;
        case 'UAH':
            $label = "грн.";
            break;
        default:
            $label = "$";
    } // switch

    return $label;
}

function format_currency_full($amount)
{
    if (null === $amount)
    {
        return null;
    }

    $currency = sfContext::getInstance()->getUser()->hasAttribute( 'currency' ) ? sfContext::getInstance()->getUser()->getAttribute( 'currency' ) : sfConfig::get( 'app_default_currency' );

    switch( $currency )
    {
        case 'USD':
            $amount = "$ ".format_currency( $amount );
            break;
        case 'EUR':
            $amount = "€ ".format_currency( $amount );
            break;
        case 'RUB':
            $amount = format_currency( $amount )." руб.";
            break;
        case 'UAH':
            $amount = format_currency( $amount )." грн.";
            break;
        default:
            $amount = "$ ".format_currency( $amount );
    } // switch

    return $amount;
}

function _current_language($culture)
{
    return $culture ? $culture : sfContext::getInstance()->getUser()->getCulture();
}
